//
//  UIAlertController+AlertController.m
//  WestWolves
//
//  Created by JL on 16/2/24.
//  Copyright © 2016年 ZJ. All rights reserved.
//

#import "UIAlertController+AlertController.h"

@implementation UIAlertController (AlertController)
+(instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message  preferredStyle:(UIAlertControllerStyle)preferredStyle andCancelAction:(void (^)(void))cancelAction andOtherAction:(void (^)(int tag))otherAction cancelButtonTitle:(NSString *)cancelTitle  otherButtonTitles:(NSString *)otherTitles,...
{
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    if (cancelTitle) {
        UIAlertAction *action=[UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (cancelAction) {
                cancelAction();
            }
        }];
        [alert addAction:action];
    }
    
    va_list args;
    va_start(args, otherTitles);
    if(otherTitles){
        int i=0;
        UIAlertAction *action=[UIAlertAction actionWithTitle:otherTitles style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (otherAction) {
                otherAction(i);
            }
        }];
        [alert addAction:action];
        i++;
        NSString *title;//otherTitles是第一个对象 循环获得剩下的对象
        while ((title = va_arg(args, id))) {
            UIAlertAction *actions=[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (otherAction) {
                    otherAction(i);
                }
            }];
            [alert addAction:actions];
            i++;
        }
    }
    va_end(args);
    return alert;
}

+(instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message  preferredStyle:(UIAlertControllerStyle)preferredStyle andCancelAction:(void (^)(void))cancelAction andOtherAction:(void (^)(int tag))otherAction cancelButtonTitle:(NSString *)cancelTitle  otherButtonInArray:(NSArray *)others
{
    UIAlertController *alert=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:preferredStyle];
    if (cancelTitle) {
        UIAlertAction *action=[UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            if (cancelAction) {
                cancelAction();
            }
        }];
        [alert addAction:action];
    }
    
    for (int i = 0; i<others.count; i++) {
        UIAlertAction *action=[UIAlertAction actionWithTitle:others[i] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (otherAction) {
                otherAction(i);
            }
        }];
        [alert addAction:action];
    }
    return alert;
}

@end
